<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>SGD | Chen_Blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="随机梯度下降法随机梯度下降（Stochastic Gradient Decent）是对全批量梯度下降法计算效率的改进算法。从本质上来说，预期的随机梯度下降算法的结果和全批量梯度下降法相接近。$$\beta^{i+1} = \beta^{i}-\alpha \triangledown L_{\beta^i}$$此处的loss函数使用的是最小二乘法。$$\triangledown L">
<meta name="keywords" content="Machine Learning">
<meta property="og:type" content="article">
<meta property="og:title" content="SGD">
<meta property="og:url" content="http://yoursite.com/2018/12/18/SGD/index.html">
<meta property="og:site_name" content="Chen_Blog">
<meta property="og:description" content="随机梯度下降法随机梯度下降（Stochastic Gradient Decent）是对全批量梯度下降法计算效率的改进算法。从本质上来说，预期的随机梯度下降算法的结果和全批量梯度下降法相接近。$$\beta^{i+1} = \beta^{i}-\alpha \triangledown L_{\beta^i}$$此处的loss函数使用的是最小二乘法。$$\triangledown L">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-12-18T08:02:09.244Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="SGD">
<meta name="twitter:description" content="随机梯度下降法随机梯度下降（Stochastic Gradient Decent）是对全批量梯度下降法计算效率的改进算法。从本质上来说，预期的随机梯度下降算法的结果和全批量梯度下降法相接近。$$\beta^{i+1} = \beta^{i}-\alpha \triangledown L_{\beta^i}$$此处的loss函数使用的是最小二乘法。$$\triangledown L">
  
    <link rel="alternate" href="/atom.xml" title="Chen_Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Chen_Blog</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">业精于勤，荒于嬉；行成于思，毁于随。</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="stochastic-SGD" class="article article-type-stochastic" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2018/12/18/SGD/" class="article-date">
  <time datetime="2018-12-18T06:11:28.000Z" itemprop="datePublished">2018-12-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      SGD
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="随机梯度下降法"><a href="#随机梯度下降法" class="headerlink" title="随机梯度下降法"></a>随机梯度下降法</h1><p>随机梯度下降（Stochastic Gradient Decent）是对全批量梯度下降法计算效率的改进算法。从本质上来说，预期的随机梯度下降算法的结果和全批量梯度下降法相接近。$$\beta^{i+1} = \beta^{i}-\alpha \triangledown L_{\beta^i}$$<br>此处的loss函数使用的是最小二乘法。<br>$$\triangledown L = (\frac{\partial L}{\partial \beta_0},\frac{\partial L}{\partial \beta_1})=(2(\beta_0+\beta_1<em>x_r-y_r),2</em>x_r(\beta_0+\beta_1*x_r-y_r))$$<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line">train = pd.read_csv(<span class="string">"train.csv"</span>)</span><br><span class="line">test = pd.read_csv(<span class="string">"test.csv"</span>)</span><br><span class="line">submit = pd.read_csv(<span class="string">"sample_submit"</span>)</span><br><span class="line"><span class="comment">#初始设置</span></span><br><span class="line">beta = [<span class="number">1</span>,<span class="number">1</span>]</span><br><span class="line">alpha = <span class="number">0.2</span></span><br><span class="line">tol_L = <span class="number">0.1</span></span><br><span class="line"><span class="comment"># 对x进行归一化</span></span><br><span class="line">max_x = max(train[<span class="string">'id'</span>])</span><br><span class="line">x = train[<span class="string">'id'</span>]/max_x</span><br><span class="line">y = train[<span class="string">'questions'</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义计算随机梯度的函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">compute_grad_SGD</span><span class="params">(beta,x,y)</span>:</span></span><br><span class="line">    grad = [<span class="number">0</span>,<span class="number">0</span>]</span><br><span class="line">    r = np.random.randint(<span class="number">0</span>,len(x))</span><br><span class="line">    grad[<span class="number">0</span>] = <span class="number">2.</span> * np.mean(beta[<span class="number">0</span>] + beta[<span class="number">1</span>] * x[r] - y[r])</span><br><span class="line">    grad[<span class="number">1</span>] = <span class="number">2.</span> * x[r] (beta[<span class="number">0</span>] + beta[<span class="number">1</span>] * x[r] -y[r])</span><br><span class="line">    <span class="keyword">return</span> np.array(grad)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义更新beta</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">update_beta</span><span class="params">(beta,alpha,grad)</span>:</span></span><br><span class="line">    new_beta = beta - alpha * grad</span><br><span class="line">    <span class="keyword">return</span> new_beta</span><br><span class="line"></span><br><span class="line"><span class="comment">#定义计算RMSE函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">rmse</span><span class="params">(beta,x,y)</span>:</span></span><br><span class="line">    squared_err = (beta_0 + beta_1 * x - y) ** <span class="number">2</span></span><br><span class="line">    res = np.sqrt(np.mean(squared_err))</span><br><span class="line">    <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line"><span class="comment">#进行初步的计算</span></span><br><span class="line">np.random.seed(<span class="number">2</span>)</span><br><span class="line">grad = compute_grad_SGD(beta,x,y)</span><br><span class="line">loss = rmse(beta,x,y)</span><br><span class="line">beta = update_beta(beta,alpha,grad)</span><br><span class="line">loss_new = rmse(beta,x,y)</span><br><span class="line"></span><br><span class="line"><span class="comment">#开始迭代</span></span><br><span class="line">i = <span class="number">1</span></span><br><span class="line"><span class="keyword">while</span> np.abs(loss_new - loss) &gt; tol_L:</span><br><span class="line">    beta = update_beta(beta,alpha,grad)</span><br><span class="line">    grad = compute_grad_SGD(beta,x,y)</span><br><span class="line">    <span class="keyword">if</span> i % <span class="number">100</span> == <span class="number">0</span>:</span><br><span class="line">        loss = loss_new</span><br><span class="line">        loss_new = rmse(beta,x,y)</span><br><span class="line">        print(<span class="string">"Round %s Diff RMSE %s "</span>%(i,np.abs(loss_new-loss)))</span><br><span class="line">    i+=<span class="number">1</span></span><br><span class="line">print(<span class="string">'Coef: %s \nIntercept %s'</span>%(beta[<span class="number">1</span>]/max_x, beta[<span class="number">0</span>]))</span><br></pre></td></tr></table></figure></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2018/12/18/SGD/" data-id="cjptgn1l50001ev15zi6qpbhg" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Machine-Learning/">Machine Learning</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2018/12/18/gradient-descent/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">gradient_descent</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Machine-Learning/">Machine Learning</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linear-regression/">linear regression</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/Machine-Learning/" style="font-size: 20px;">Machine Learning</a> <a href="/tags/linear-regression/" style="font-size: 10px;">linear regression</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">December 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">August 2018</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">July 2018</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2018/12/18/SGD/">SGD</a>
          </li>
        
          <li>
            <a href="/2018/12/18/gradient-descent/">gradient_descent</a>
          </li>
        
          <li>
            <a href="/2018/12/06/linear-regression-gluon/">linear-regression-gluon</a>
          </li>
        
          <li>
            <a href="/2018/12/06/linear-regression-sources-md/">linear-regression-sources.md</a>
          </li>
        
          <li>
            <a href="/2018/12/05/regression/">regression</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2018 alex Chen<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>



  </div>
</body>
</html>